package com.zmn.plat.dubbo.impl.orderconfig.serviceaudit;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.cglib.beans.BeanCopier;

import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.orderconfig.serviceaudit.OrderServiceAuditReasonDIO;
import com.zmn.plat.common.dto.orderconfig.serviceaudit.OrderServiceAuditReasonDRO;
import com.zmn.plat.dubbo.interfaces.orderconfig.serviceaudit.OrderServiceAuditReasonListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.orderconfig.serviceaudit.OrderServiceAuditReason;
import com.zmn.plat.model.entity.orderconfig.serviceaudit.OrderServiceAuditReasonQuery;
import com.zmn.plat.services.interfaces.orderconfig.serviceaudit.OrderServiceAuditReasonService;
import com.zmn.plat.utils.bean.BeanCopyUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @author tanbiao
 * @description：服务审核取消缘由
 * @since 2019/10/21 18:17
 * @modifier
 **/
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
@Slf4j
public class OrderServiceAuditReasonListRemoteServiceImpl implements OrderServiceAuditReasonListRemoteService {
    private static final String TAG = "订单取消缘由 dubbo 接口";
    @Resource
    private OrderServiceAuditReasonService orderServiceAuditReasonService;

    /**
     * 描述: 根据主键查询取消缘由
     *
     * @param id
     * @return OrderCancelCategoryDRO
     * @auther: tanbiao
     * @since: 2020/09/17 11:11
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<OrderServiceAuditReasonDRO> getById(Integer id) {
        log.info("getById 参数为 id={}", id);
        OrderServiceAuditReason reason = orderServiceAuditReasonService.findByKey(id);
        if (reason == null) {
            log.error(String.format("[%s]参数:[%s]对应的对象不存在", TAG, "productId"));
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }

        OrderServiceAuditReasonDRO dro = new OrderServiceAuditReasonDRO();
        BeanCopyUtils.copyProperties(reason, dro);
        dro.setBizTypeName(com.zmn.plat.common.dictionary.ProductDict.translateBizType(dro.getBizType(), ","));
        return ResponseDTO.success(dro);
    }

    /**
     * 描述: 根据条件查询取消缘由
     *
     * @param orderServiceAuditReasonDIO
     * @return OrderCancelCategoryDRO
     * @auther: tanbiao
     * @since: 2020/09/17 11:11
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<OrderServiceAuditReasonDRO>> listByQuery(OrderServiceAuditReasonDIO orderServiceAuditReasonDIO) {
        log.info("listByQuery 参数为 orderServiceAuditReasonDIO={}", orderServiceAuditReasonDIO);

        OrderServiceAuditReasonQuery query = new OrderServiceAuditReasonQuery();
        BeanCopyUtils.copyProperties(orderServiceAuditReasonDIO, query);
        List<OrderServiceAuditReason> reasons = Optional.ofNullable(orderServiceAuditReasonService.listByQuery(query)).orElse(Collections.emptyList());

        BeanCopier copier = BeanCopier.create(OrderServiceAuditReason.class, OrderServiceAuditReasonDRO.class, false);
        List<OrderServiceAuditReasonDRO> result = reasons.stream().map(reason -> {
            OrderServiceAuditReasonDRO dro = new OrderServiceAuditReasonDRO();
            copier.copy(reason, dro, null);
            dro.setBizTypeName(com.zmn.plat.common.dictionary.ProductDict.translateBizType(dro.getBizType(), ","));
            return dro;
        }).collect(Collectors.toList());

        return ResponseDTO.success(result);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<VtDTO>> listVtByStatus(Integer status) {
        log.info("listVtByStatus 参数为 status={}", status);
        OrderServiceAuditReasonQuery query = new OrderServiceAuditReasonQuery();
        query.setStatus(status);
        List<OrderServiceAuditReason> list = Optional.ofNullable(orderServiceAuditReasonService.listByQuery(query)).orElse(Collections.emptyList());

        List<VtDTO> items = list.stream().map(item -> new VtDTO(item.getId(), item.getReason(), item.getStatus())).collect(Collectors.toList());

        return ResponseDTO.success(items);
    }
}
